/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_flowing_deque
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 /**
   * @file        jh_flowing_deque.c
   * @brief       流动双端队列
   * @details     流动双端队列
   * @author      Hong Jiahua
   * @version     1.0.0
   * @date        2022-03-15
   * @copyright   2022 Hong Jiahua MIT License
   */
#include "jh_flowing_deque.h"

/************************************************************************/
/*                                                                      */
/************************************************************************/
/**
  * @brief      判断流动双端队列是否为空
  * @param[in]  deque   流动双端队列
  *
  * @return     errcode
  * @retval      1      流动双端队列为空
  * @retval      0      流动双端队列不为空
  * @retval     -1      失败
  */
int jh_flowing_deque_is_empty(jh_flowing_deque_t* deque) {
    if(deque == NULL)
        return -1;
    
    return deque->head == deque->tail;
}

/**
  * @brief      判断流动双端队列是否已满
  * @param[in]  deque   流动双端队列
  *
  * @return     errcode
  * @retval      1      流动双端队列已满
  * @retval      0      流动双端队列未满
  * @retval     -1      失败
  */
int jh_flowing_deque_is_full(jh_flowing_deque_t* deque) {
    if(deque == NULL)
        return -1;
    
    return (deque->tail + 1) % deque->num == deque->head;
}

/**
  * @brief      获取流动双端队列的数据数量
  * @param[in]  deque   流动双端队列
  *
  * @return     errcode
  * @retval     >=0     流动双端队列的数据数量
  */
size_t jh_flowing_deque_count(jh_flowing_deque_t* deque) {
    if(deque == NULL)
        return 0;
    
    return (deque->tail + deque->num - deque->head) % deque->num;
}

/**
  * @brief      数据添加到队首
  * @param[in]  deque   流动双端队列
  * @param[in]  item    要入队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_push_head(jh_flowing_deque_t* deque, void* item) {
    if(deque == NULL || item == NULL)
        return -1;
    
    void* out = NULL;
    
    if((deque->tail + 1) % deque->num == deque->head) {
        deque->tail = deque->tail ? deque->tail - 1 : deque->num - 1;
        out = deque->base + deque->tail * deque->size;
    }
    
    deque->head = deque->head ? deque->head - 1 : deque->num - 1;
    memcpy(deque->base + deque->head * deque->size, item, deque->size);
    
    if(deque->handle)
        deque->handle(item, out, deque->user_data);
    
    return 0;
}

/**
  * @brief      数据添加到队尾
  * @param[in]  deque   流动双端队列
  * @param[in]  item    要入队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_push_tail(jh_flowing_deque_t* deque, void* item) {
    if(deque == NULL || item == NULL)
        return -1;
    
    void* out = NULL;
    
    if((deque->tail + 1) % deque->num == deque->head) {
        out = deque->base + deque->head * deque->size;
        deque->head = (deque->head + 1) % deque->num;
    }
    
    memcpy(deque->base + deque->tail * deque->size, item, deque->size);
    deque->tail = (deque->tail + 1) % deque->num;
    
    if(deque->handle)
        deque->handle(item, out, deque->user_data);
    
    return 0;
}

/**
  * @brief      队首数据出队
  * @param[in]  deque   流动双端队列
  * @param[in]  item    要出队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_pop_head(jh_flowing_deque_t* deque, void* item) {
    if(deque == NULL || item == NULL || deque->head == deque->tail)
        return -1;
    
    memcpy(item, deque->base + deque->head * deque->size, deque->size);
    deque->head = (deque->head + 1) % deque->num;
    
    if(deque->handle)
        deque->handle(NULL, item, deque->user_data);
    
    return 0;
}

/**
  * @brief      队尾数据出队
  * @param[in]  deque   流动双端队列
  * @param[in]  item    要出队的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_pop_tail(jh_flowing_deque_t* deque, void* item) {
    if(deque == NULL || item == NULL || deque->head == deque->tail)
        return -1;
    
    deque->tail = deque->tail ? deque->tail - 1 : deque->num - 1;
    memcpy(item, deque->base + deque->tail * deque->size, deque->size);
    
    if(deque->handle)
        deque->handle(NULL, item, deque->user_data);
    
    return 0;
}

/**
  * @brief      查看队首的数据
  * @param[in]  deque   流动双端队列
  * @param[in]  item    队首的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_peek_head(jh_flowing_deque_t* deque, void* item) {
    if(deque == NULL || item == NULL || deque->head == deque->tail)
        return -1;
    
    memcpy(item, deque->base + deque->head * deque->size, deque->size);
    
    return 0;
}

/**
  * @brief      查看队尾的数据
  * @param[in]  deque   流动双端队列
  * @param[in]  item    队尾的数据项
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_peek_tail(jh_flowing_deque_t* deque, void* item) {
    if(deque == NULL || item == NULL || deque->head == deque->tail)
        return -1;
    
    memcpy(item, deque->base + (deque->tail ? (deque->tail - 1) : (deque->num - 1)) * deque->size, deque->size);
    
    return 0;
}

/**
  * @brief      清空流动双端队列
  * @param[in]  deque   流动双端队列
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_clear(jh_flowing_deque_t* deque) {
    if(deque == NULL)
        return -1;
    
    return deque->head = deque->tail = 0;
}

/**
  * @brief      获取流动双端队列指定位置的指定数量数据
  * @param[in]  deque   流动双端队列
  * @param[in]  item    获取的数据项
  * @param[in]  start   指定的起始位置
  * @param[in]  num     指定的数量
  *
  * @return     errcode
  * @retval     >=0     成功获取的数据数量
  * @retval     -1      失败
  */
int jh_flowing_deque_get_data(jh_flowing_deque_t* deque, void* item, size_t start, size_t num) {
    if(deque == NULL || item == NULL)
        return -1;
    
    size_t count = (deque->tail + deque->num - deque->head) % deque->num;
    if(start >= count)
        return 0;
    
    size_t vaild_len = (count - start) < num ? (count - start) : num;
    size_t all_tail_len = deque->tail > deque->head ? deque->tail - deque->head : deque->num - deque->head;
    size_t tail_len = all_tail_len > start ? ((all_tail_len - start) < vaild_len ? all_tail_len - start : vaild_len) : 0;
    memcpy(item, deque->base + (all_tail_len > start ? deque->head + start : 0) * deque->size, tail_len * deque->size);
    memcpy(item + tail_len * deque->size, deque->base + (all_tail_len > start ? 0 : start - all_tail_len) * deque->size, (vaild_len - tail_len) * deque->size);
    
    return vaild_len;
}

/**
  * @brief      流动双端队列初始化
  * @param[in]  deque   流动双端队列
  * @param[in]  base    指向用于流动双端队列的数组
  * @param[in]  num     数组中能存放的数据项数量
  * @param[in]  size    数组中每个数据项的大小
  *
  * @return     errcode
  * @retval      0      成功
  * @retval     -1      失败
  */
int jh_flowing_deque_init(jh_flowing_deque_t* deque, void* base, size_t num, size_t size, void* user_data, void (*handle)(void*, void*, void*)) {
    if(deque == NULL || base == NULL)
        return -1;
    
    deque->base = base;
    deque->num = num;
    deque->size = size;
    deque->head = 0;
    deque->tail = 0;
    
    deque->user_data = user_data;
    deque->handle = handle;
    
    return 0;
}
